package com.fhz.a2102shopjava.ui.goods;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fhz.a2102shopjava.R;
import com.fhz.a2102shopjava.entity.Category;
import com.fhz.a2102shopjava.entity.Orders;
import com.fhz.a2102shopjava.entity.ShoppingCart;
import com.fhz.a2102shopjava.util.ImageLoader;

import java.util.List;

/**
 * 时间:2023/8/16
 *
 * @author Mr.Feng
 * 简述: 购物车中的布局
 */
public class GoodsItemRecyclerViewAdapter extends RecyclerView.Adapter<GoodsItemRecyclerViewAdapter.ViewHolder> {

    private Context mContext;
    private List<ShoppingCart> dataList;

    private static final String NUM = "num";

    public GoodsItemRecyclerViewAdapter(Context context, List<ShoppingCart> dataList) {
        mContext = context;
        this.dataList = dataList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView goodsItemTitle;
        private TextView goodsItemPrice;
        private TextView dgoodsItemNum;
        private ImageView goodsItemPhoto;
        private ImageView goodsItemMinus;
        private ImageView goodsItemAdd;
        private CheckBox goodsRadio;
        private OnItemClickListener mListener;// 声明自定义监听接口
        private OnSubItemClickListener mOnSubItemClickListenerListener;// 声明自定义子View监听接口

        ViewHolder(@NonNull View itemView,OnItemClickListener listener,OnSubItemClickListener onSubItemClickListener) {
            super(itemView);
            mListener = listener;
            mOnSubItemClickListenerListener = onSubItemClickListener;
            itemView.setOnClickListener(this); // 为ItemView添加点击事件
            goodsItemTitle = itemView.findViewById(R.id.goods_item_title);
            goodsItemPhoto = itemView.findViewById(R.id.goods_item_photo);
            goodsItemMinus = itemView.findViewById(R.id.goods_item_minus);
            goodsItemAdd = itemView.findViewById(R.id.goods_item_add);
            dgoodsItemNum = itemView.findViewById(R.id.goods_item_num);
            goodsItemPrice = itemView.findViewById(R.id.goods_item_price);
            goodsRadio = itemView.findViewById(R.id.goods_item_radio);

            goodsItemAdd.setOnClickListener(v -> mOnSubItemClickListenerListener.onSubItemClick(goodsItemAdd.getId(),getLayoutPosition()));
            goodsItemMinus.setOnClickListener(v -> mOnSubItemClickListenerListener.onSubItemClick(goodsItemMinus.getId(),getLayoutPosition()));
            goodsRadio.setOnClickListener(v -> mOnSubItemClickListenerListener.onSubItemClick(goodsRadio.getId(),getLayoutPosition()));
        }

        @Override
        public void onClick(View v) {
            mListener.onItemClick(v, getLayoutPosition());
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_goods_rv_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view,mClickListener,mOnSubItemClickListener);
        return viewHolder;
    }

    /**
     * 普通使用
     * @param holder The ViewHolder which should be updated to represent the contents of the
     *        item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShoppingCart shop = dataList.get(position);
        holder.goodsItemTitle.setText(shop.getCarTitle().substring(0,14));
        holder.dgoodsItemNum.setText(String.valueOf(shop.getCarNum()));
        holder.goodsItemPrice.setText(shop.getCarPrice());
        ImageLoader.load(holder.goodsItemPhoto.getContext(), shop.getCarImage(), holder.goodsItemPhoto);
        holder.goodsRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                shop.setCheck(isChecked);
            }
        });
        dataList.set(position,shop);

    }

    /**
     * 可局部刷新,整页刷新图片闪烁,可去除动画,局部刷新不闪,效率还高
     * @param holder The ViewHolder which should be updated to represent the contents of the
     *               item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     * @param payloads A non-null list of merged payloads. Can be empty list if requires full
     *                 update.
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        }
        else {
            for (Object payload : payloads) {
                if (payload instanceof Integer) {
                    ShoppingCart shoppingCart = dataList.get(position);
                    shoppingCart.setCarNum((int)payload);
                    dataList.set(position,shoppingCart);
                    holder.dgoodsItemNum.setText(String.valueOf(shoppingCart.getCarNum()));
                    System.out.println("局部更新成功!");
                }else if(payload instanceof Boolean){
                    if ((Boolean) payload){
                        holder.goodsRadio.setVisibility(View.VISIBLE);
                    }
                }
            }
        }

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private OnItemClickListener mClickListener;//自定义的接口

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setItemClickListener(OnItemClickListener clickListener) {
        mClickListener = clickListener;
    }
    private OnSubItemClickListener mOnSubItemClickListener;//自定义的接口


    public interface OnSubItemClickListener {
        void onSubItemClick(int id,int position);
    }

    public void setOnSubItemClickListener(OnSubItemClickListener onSubItemClickListener) {
        mOnSubItemClickListener = onSubItemClickListener;
    }
}
